<template>
  <div class="app-container">
    <div class="div-sch">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="客户编号" prop="customerId">
          <el-input
            v-model="queryParams.customerId"
            placeholder="请输入客户编号"
            clearable
            style="width: 240px;"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="div-t">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['system:user:add']"
          >新增防疫信息</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table ref="tables"  border v-loading="loading" :height="tableHeight"  :data="list" @selection-change="handleSelectionChange" :default-sort="defaultSort" @sort-change="handleSortChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="客户编号" minWidth="120%" align="center" prop="customerId" />
        <el-table-column label="禁止通行健康码"  align="center" prop="healthCode" >
          <template slot-scope="scope">
             <el-tag 
               v-for="item in setTag(scope.row)"
                :key="item.index"
                size="mini" 
                :type="item=='红'?'danger':item=='黄'?'warning':'success'"
              >{{item}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="禁止通行行程码"  align="center" prop="tripCode" />
        <el-table-column label="核酸有效期" align="center" prop="nucleicAcid">
          <template slot-scope="scope">
            {{scope.row.nucleicAcid+"h"}}
          </template>
        </el-table-column>
        <el-table-column label="操作"  align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['monitor:operlog:query']"
            >编辑</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              style="color:red"
              @click="handleDelete(scope.row)"
              v-hasPermi="['monitor:operlog:query']"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改标签的对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="150px">
          <el-form-item label="禁止通行健康码" prop="healthCode">
            <el-select
              v-model="form.healthCode"
              placeholder="请选择禁止通行健康码"
              clearable
              multiple
              filterable
            >
            <el-option value=红 label="红"></el-option>
            <el-option value=黄 label="黄"></el-option>
            <el-option value=绿 label="绿"></el-option>
          </el-select>
          </el-form-item>
          <el-form-item label="禁止通行行程码" prop="tripCode">
            <el-select
              v-model="form.tripCode"
              placeholder="请选择禁止通行行程码"
              clearable
              multiple
              filterable

              >
              <el-option value="上海市" label="上海市"></el-option>
              <el-option value="无锡市" label="无锡市"></el-option>
              <el-option value="郑州市" label="郑州市"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="核酸有效期" prop="nucleicAcid">
            <el-select
              v-model="form.nucleicAcid"
              placeholder="请选择核酸有效期"
              clearable
              filterable
              >
              <el-option value=24 label="24h"></el-option>
              <el-option value=48 label="48h"></el-option>
              <el-option value=72 label="72h"></el-option>
            </el-select>
          </el-form-item>
          
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { addTag,updateTag,batchDeleteTag } from "@/api/business/tag";
import { epidemicApi } from "@/api/business/epidemic";

export default {
  name: "Operlog",
  dicts: ['sys_oper_type', 'sys_common_status'],
  data() {
    return {
      tableHeight:0,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中标签id数组
      tagIds: [],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 默认排序
      defaultSort: {prop: 'operTime', order: 'descending'},
      // 表单参数
      // 弹出层标题
      title: "",
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        customerId: undefined
      },
      // 表单校验
      rules: {
        healthCode: [
          { required: true, message: "请选择禁止通行健康码", trigger: "blur" }
        ],
        tripCode: [
          { required: true, message: "请选择禁止通行行程码", trigger: "blur" }
        ],
        nucleicAcid: [
          { required: true, message: "请选择核酸有效期", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getTableHeight();
  },
  methods: {
    getTableHeight(){
      this.tableHeight = window.innerHeight - 300;
    },
    setTag(row){
        if(row.healthCode!=null){
          const healthCode = JSON.parse(row.healthCode)
          return healthCode;
       }else{
         return [];
       }
    },
    /** 查询 */
    getList() {
      this.loading = true;
      epidemicApi.getEpidemicList(this.queryParams).then( res => {
          this.list = res.data.records;
          this.total = res.data.total;
          this.loading = false;
        }
      );
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.$refs.tables.sort(this.defaultSort.prop, this.defaultSort.order)
      this.handleQuery();
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.multiple = !selection.length
    },
    /** 排序触发事件 */
    handleSortChange(column, prop, order) {
      this.queryParams.orderByColumn = column.prop;
      this.queryParams.isAsc = column.order;
      this.getList();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加防疫信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.title = "修改防疫信息";
      this.form.healthCode = row.healthCode;
      this.form.tripCode = row.tripCode;
      this.form.nucleicAcid = row.nucleicAcid;
      
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除？',"提示").then(res=>{
        epidemicApi.deleteEpidemic(row.id).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        });
      })
    },
    // 表单重置
    reset() {
      this.form = {};
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            epidemicApi.updateEpidemic(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            epidemicApi.addEpidemic(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

  }
};
</script>
<style  >
.div-t {
  background: white;
  padding: 10px 10px 20px 10px;
  border-radius: 5px;
  /* border: 1px solid rgb(211, 208, 208); */
}
.div-sch {
  background: white;
  padding: 10px 10px 0px 10px;
  border-radius: 5px;
  margin-bottom: 10px;
}
</style>